package com.cspg.ssss.controller;

import com.cspg.ssss.domain.*;
import com.cspg.ssss.service.*;
import com.cspg.ssss.vo.ProblemGradeViewVo;
import com.cspg.ssss.vo.ResultVo;
import com.cspg.ssss.vo.ResultVoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/daiban")
public class DaiBanController {

    @Autowired
    RectifyService rectifyService;
    @Autowired
    private DaiBanService daiBanservice;
    @Autowired
    ProblemGradeService problemGradeService;
    @Autowired
    ScoreAddApplyService scoreAddApplyService;
    @Autowired
    CheckBatchService checkBatchService;

    @GetMapping("/selectInst")
    public ResultVo selectInst() {
        Map<String, Object> data = new HashMap<>();
        data.put("gsInst", checkBatchService.getCurrentCheckBachInst(0));
        data.put("dwInst", checkBatchService.getCurrentCheckBachInst(1));
        return ResultVoUtil.success("查询成功", data, "400");
    }

    @GetMapping("/selectList")
    public ResultVo selectList(@RequestParam Map<String, Object> map) {
        List<BacklogEntity> list = daiBanservice.selectdb(map);
        Map<String, Object> data = new HashMap<>();
        data.put("list", list);
        int read = 0;
        int term = 0;
        for (BacklogEntity entity : list) {
            read += entity.getUpdatetime() == null ? 1 : 0;
            term += "2".equals(entity.getTerm()) ? 1 : 0;
        }
        data.put("read", read);
        data.put("term", term);
        return ResultVoUtil.success("查询成功", data, "400");
    }

    //经办列表
    @PostMapping("/selectHandlingList")
    public ResultVo selectHandlingList(@RequestBody Map<String, Object> map) {
        List<BackHaveEntity> page = daiBanservice.selectHandlingList(map);
        return ResultVoUtil.success("查询成功", page, "400");
    }

    //发起列表
    @PostMapping("/selectLaunchList")
    public ResultVo selectLaunchList(@RequestBody Map<String, Object> map) {
        List<BackHaveEntity> page = daiBanservice.selectLaunchList(map);
        return ResultVoUtil.success("查询成功", page, "400");
    }

    //已办详情
    @PostMapping("/detail")
    public ResultVo detail(@RequestBody Map<String, Object> map) {
        if ("0".equals(map.get("objType"))) {
            int checkType = Integer.parseInt(map.get("checkType").toString());
            Map<String, Object> condition = new HashMap<>();
            condition.put("id", map.get("problemId"));
            List<ProblemGradeViewVo> retls = problemGradeService.getProblemGradeViewAllWithCondition(checkType, condition);
            if (retls.size() > 0) {
                return ResultVoUtil.success("查询成功", retls.get(0), "400");
            }
        } else if ("1".equals(map.get("objType"))) {
            ScoreAddApplyEntity en = scoreAddApplyService.getScoreAddApply(map.get("problemId").toString());
            return ResultVoUtil.success("查询成功", en, "400");
        }
        if ("1".equals(map.get("opType"))) {
            Map<String, Object> condition = new HashMap<>();
            condition.put("problem_type", map.get("checkType"));
            condition.put("id", map.get("problemId"));
            List<CompanyRectifyViewEntity> companyList = rectifyService.getCompanyRectifyListByConditionNoPage(condition);
            if (companyList.size() > 0) {
                return ResultVoUtil.success("查询成功", companyList.get(0), "400");
            } else {
                List<DeptRectifyViewEntity> deptList = rectifyService.getDeptRectifyListByConditionNoPage(condition);
                if (deptList.size() > 0) {
                    return ResultVoUtil.success("查询成功", deptList.get(0), "400");
                }
            }
        }
        return null;
    }

    @PostMapping("/update")
    public ResultVo update(@RequestParam String id) {
        BacklogEntity entity = new BacklogEntity();
        entity.setId(Integer.parseInt(id));
        entity.setUpdatetime(new Date());
        daiBanservice.updateBacklog(entity);
        return ResultVoUtil.success("修改成功", null);
    }

    @GetMapping(value = "/test")
    public ResultVo test() throws ParseException {
        //更新公司待办事项
        daiBanservice.updateBacklogList(0);
//        //更新单位待办事项
        daiBanservice.updateBacklogList(1);
        return ResultVoUtil.success("查询成功", null);
    }
}
